package com.briup.estore.service;

import com.briup.estore.bean.Shopcar;
import com.briup.estore.bean.ext.ShopCarExtend;
import com.briup.estore.mapper.ShopcarMapper;
import com.briup.estore.utils.BeanCopyUtils;
import com.briup.estore.vo.ShopCarVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ShopCarService {
    @Resource
    private ShopcarMapper shopcarMapper;

    public void add(int userId, Integer bookId) {
        Shopcar shopcardb = shopcarMapper.findByUserIdAndBookId(userId, bookId);
        if (shopcardb == null) {
           Shopcar shopcar = new Shopcar();
           shopcar.setUserId(userId).setBookId(bookId).setNum(1);
           shopcarMapper.addCar(shopcar);
        } else {
            shopcardb.setNum(shopcardb.getNum()+1);
            shopcarMapper.updateCar(shopcardb);
        }
    }

    public List<ShopCarVO> findLists(Integer userId) {
        List<ShopCarExtend> list = shopcarMapper.findByUserId(userId);
        List<ShopCarVO> shopCarVOS = BeanCopyUtils.copyBeanList(list,
                ShopCarVO.class);
        return shopCarVOS;
    }

    public void deleteshopCar(List<Integer> shopCarlist) {
        shopcarMapper.deleteshopCar(shopCarlist);
    }
}